package gamecode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class EntityPlayerShip extends Entity {

	public EntityPlayerShip() {
		// Init player ship
	}
	
	private Polygon getShipPolygon(Rectangle bounds) {
		Polygon p = new Polygon();
		int scale = 10;
		double angle = 150;
		p.addPoint((int)Vector2D.getXY(this.getDirection(), scale).getX(), (int)Vector2D.getXY(this.getDirection(), scale).getY());
		p.addPoint((int)Vector2D.getXY(this.getDirection()+angle, scale).getX(), (int)Vector2D.getXY(this.getDirection()+angle, scale).getY());
		p.addPoint((int)Vector2D.getXY(this.getDirection()-angle, scale).getX(), (int)Vector2D.getXY(this.getDirection()-angle, scale).getY());
		p.translate(bounds.width/2, bounds.height/2);
		return p;
	}
	
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == e.VK_A) {
			this.rotateAcceleration(-maxShipTurnSpeed);
		}
		else if(e.getKeyCode() == e.VK_D) {
			this.rotateAcceleration(maxShipTurnSpeed);
		}
		// TODO: Fix and finish ship movement
		/*else if(e.getKeyCode() == e.VK_W) {
			this.setAcceleration(this.getDirection(), 0.1);
		}
		else if(e.getKeyCode() == e.VK_S) {
			this.setAcceleration(this.getDirection(), -0.1);
		}*/
	}
	
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode() == e.VK_A) {
			this.rotateAcceleration(0);
		}
		else if(e.getKeyCode() == e.VK_D) {
			this.rotateAcceleration(0);
		}
		/*else if(e.getKeyCode() == e.VK_W) {
			this.setAcceleration(this.getDirection(), 0);
		}
		else if(e.getKeyCode() == e.VK_S) {
			this.setAcceleration(this.getDirection(), 0);
		}*/
	}
	
	public void rotateLeft() {
		this.rotate(-maxShipTurnSpeed);
	}
	
	public void rotateRight() {
		this.rotate(maxShipTurnSpeed);
	}
	
	public void drawShip(Graphics2D g2d, Rectangle bounds) {
		g2d.setColor(Color.GREEN);
		g2d.drawPolygon(getShipPolygon(bounds));
	}
}